package com.example.zq.player.linked_database_interface;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.zq.player.R;
import com.example.zq.player.activity.MainActivity;
import com.example.zq.player.linked_database_interface.base.RequestInterface;
import com.example.zq.player.linked_database_interface.base.ServerResponse;
import com.example.zq.player.linked_database_interface.bean.User;
import com.example.zq.player.pager.MyPager;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.login);

        Button login_btn = findViewById(R.id.login_btn);
        Button registered_btn = findViewById(R.id.registered_btn);

        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str_username = ((EditText)findViewById(R.id.login_username)).getText().toString();
                String str_password = ((EditText)findViewById(R.id.login_password)).getText().toString();

                RequestInterface.LoginInterface requestInterfaceLogin = GlobalInformation.getRetrofit().create(RequestInterface.LoginInterface.class);
                Call<ServerResponse<User>> call = requestInterfaceLogin.login(str_username, str_password);

                call.enqueue(new Callback<ServerResponse<User>>() {

                    @Override
                    public void onResponse(Call<ServerResponse<User>> call, Response<ServerResponse<User>> response) {
                        if (response.body() == null) {
                            Toast.makeText(LoginActivity.this, "网络异常，请检查网络配置", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        //log(response.body().getMsg());
                        if (response.body().getStatus() == 0) {
                            //获取返回的数据（就是泛型ServerResponse中的T所代表的数据）
                            User user = response.body().getData();
                            GlobalInformation.setUser(user);
//                            Toast.makeText(LoginActivity.this, "登录成功！"+user.toString(), Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            LoginActivity.this.startActivity(intent);
                            LoginActivity.this.finish();
                        } else {
                            Toast.makeText(LoginActivity.this, response.body().getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<ServerResponse<User>> call, Throwable t) {
                        Toast.makeText(LoginActivity.this, "登陆失败！", Toast.LENGTH_SHORT).show();
                        Log.d("mError", "LoginFailure: ");
                    }
                });
            }
        });

        registered_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                LoginActivity.this.startActivity(intent);
                LoginActivity.this.finish();
            }
        });
    }
}
